﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/sys/user"
@inject ISysUserService SysUserService
@namespace ThingsGateway.Web.Rcl
@attribute [Authorize]
@attribute [RolePermission]
@inherits CultureComponentBase
@layout MainLayout
@inject MasaBlazor MasaBlazor
@inject UserResoures UserResoures
<AppDataTable @ref="_datatable" TItem="SysUser" SearchItem="UserPageInput"
              AddItem="UserAddInput" EditItem="UserEditInput" SearchModel="@search"
              QueryCall="QueryCall" AddCall="AddCall" EditCall="EditCall" DeleteCall="DeleteCall"
              FilterHeaders="FilterHeaders" Filters="Filters"
                ShowQueryButton ShowDetailButton
              ShowAddButton=@UserResoures.IsHasButtonWithRole("sysuseradd")
              ShowDeleteButton=@UserResoures.IsHasButtonWithRole("sysuserdelete")
              ShowEditButton=@UserResoures.IsHasButtonWithRole("sysuseredit")
                IsShowSearchKey>

    <AddTemplate>
        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Account))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Account />

        <MSubheader Class="mt-4 font-weight-black">   @(context.Description(x => x.Phone))</MSubheader>
            <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Phone />

        <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Email))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Email />

        <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
        <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />

    </AddTemplate>

    <EditTemplate>
        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Account))  </MSubheader>
        <MTextField Disabled=@(context.Account == RoleConst.SuperAdmin) Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Account />

        <MSubheader Class="mt-4 font-weight-black">   @(context.Description(x => x.Phone))</MSubheader>
            <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Phone />

        <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Email))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Email />

        <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
        <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />

    </EditTemplate>

    <ItemColOperTemplate>
        <MList Dense>
            @if (@UserResoures.IsHasButtonWithRole("sysuserresetpassword"))
            {
                <MListItem OnClick="()=>ResetPassword(context.Item)">
                    <MListItemTitle Class="ml-2"> @T("重置密码") </MListItemTitle>
                </MListItem>
            }

            @if (@UserResoures.IsHasButtonWithRole("sysuserperrole"))
            {
                <MListItem OnClick="async()=>{
                    ChoiceUserId=context.Item.Id;
                    await RoleInit();
                    IsShowRoles=true;}">
                    <MListItemTitle Class="ml-2"> @T("角色授权") </MListItemTitle>
                </MListItem>
            }

        </MList>
    </ItemColOperTemplate>

    <ItemColTemplate>
        @switch (context.Header.Value)
        {
            case nameof(context.Item.UserStatus):
                <EnableSwitch ValueChanged=@(a=>UserStatusChange(context.Item,a))
                              Value="context.Item.UserStatus">
                </EnableSwitch>
                break;
            default:
                @context.Value
                break;
        }

    </ItemColTemplate>
</AppDataTable>

<PDrawer @bind-Value="IsShowRoles" OnCancel="() => IsShowRoles = false"
         Title=@T("角色授权")
         Width=@(MasaBlazor.Breakpoint.Mobile?"100%":"500")
         MaxWidth="500" OnSave="OnRolesSave">
    @if (IsShowRoles)
    {
        <MCard Flat Class="ma-0 pa-4">
            <MCardTitle Class="py-2">
                <MTextField Clearable Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 ml-3" @bind-Value="SearchName"
                            Outlined Label=@typeof(SysRole).GetDescription(nameof(SysRole.Name)) />
            </MCardTitle>
            <MTreeview Class="my-1" Dense OpenAll TItem="SysRole" TKey="SysRole" Selectable
            @bind-Value="RolesChoice" Search="@SearchName" ItemKey=@(r=>r)
                       Items="AllRoles" ItemText="r=>r.Name" ItemChildren="r=>null">
            </MTreeview>
            </MCard>
    }
</PDrawer>

@code {
    string SearchName;
    long ChoiceUserId;
    [Inject]
    IRoleService SysRoleService { get; set; }
    bool IsShowRoles;
    List<SysRole> RolesChoice = new();
    private async Task OnRolesSave(ModalActionEventArgs args)
    {
        try
        {
            UserGrantRoleInput userGrantRoleInput = new();
            userGrantRoleInput.Id = ChoiceUserId;
            userGrantRoleInput.RoleIdList = RolesChoice.Select(it => it.Id).ToList();
            await SysUserService.GrantRole(userGrantRoleInput);
            IsShowRoles = false;
        }
        catch (Exception ex)
        {
            args.Cancel();
            await PopupService.EnqueueSnackbarAsync(ex, false);
        }
    }
    private List<SysRole> AllRoles;
    private async Task RoleInit()
    {
        AllRoles = await SysRoleService.RoleSelector();
        var data = await SysRoleService.GetRoleIdListByUserId(ChoiceUserId);
        RolesChoice = AllRoles.Where(a => data.Contains(a.Id)).ToList();
    }
}